package com.xhsj.user.sms.process.service;

import com.xhsj.user.sms.process.entity.TbPurchaseProcessOpinion;
import com.xhsj.user.sms.process.mapper.TbPurchaseProcessOpinionMapper;
import com.xhsj.user.sms.base.BaseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 审核意见表 服务实现类
 *
 * @author suxiaolin
 * @since 2020-03-16
 */
@Service
@Transactional(rollbackFor = Exception.class)
// @CacheConfig(cacheNames = "TbPurchaseProcessOpinion")
public class TbPurchaseProcessOpinionService
    extends BaseService<TbPurchaseProcessOpinionMapper, TbPurchaseProcessOpinion> {
  @Autowired private TbPurchaseProcessOpinionMapper dao;

  @Override
  // @CacheEvict(cacheNames="TbPurchaseProcessOpinion", allEntries=true)
  public boolean save(TbPurchaseProcessOpinion entity) {
    return super.save(entity);
  }

  @Override
  // @Cacheable(key = "#id",sync = true)
  public TbPurchaseProcessOpinion getById(Serializable id) {
    return super.getById(id);
  }

  @Override
  // @CacheEvict(cacheNames="TbPurchaseProcessOpinion", allEntries=true)
  public boolean removeById(Serializable id) {
    return super.removeById(id);
  }

  // @CacheEvict(cacheNames="SysDict", allEntries=true)
  @Override
  public boolean removeByIds(Collection<? extends Serializable> idList) {
    return super.removeByIds(idList);
  }

  // @Cacheable(key = "#data",sync = true)
  public IPage<TbPurchaseProcessOpinion> selectPage(Map<String, Object> data) {
    int currentPage = 1;
    if (!Objects.isNull(data.get("page"))) {
      currentPage = (Integer) data.get("page");
    }
    int pageSize = 20;
    if (!Objects.isNull(data.get("size"))) {
      pageSize = (Integer) data.get("size");
    }

    TbPurchaseProcessOpinion entity =
        JSON.parseObject(JSON.toJSONString(data), TbPurchaseProcessOpinion.class);
    Page<TbPurchaseProcessOpinion> page = new Page(currentPage, pageSize);
    QueryWrapper<TbPurchaseProcessOpinion> wrapper = new QueryWrapper(entity);
    return super.selectPage(page, wrapper);
  }

  @Override
  // @Cacheable(value = "TbPurchaseProcessOpinion",sync = true)
  public List<TbPurchaseProcessOpinion> selectAll() {
    return super.selectAll();
  }

  //    获取审批意见
  public List<Map<String, Object>> findAuditOpinionList(String dataId) {
    // 获取节点id

    List<String> nodeList = new ArrayList<>();
    List<String> nodeListOpinion = new ArrayList<>();
    nodeList.add("1d44e4303a95439284cb92eefca65156");
    nodeList.add("5e8d804d54b2485391d10231fdddf4cc");
    nodeList.add("71a072f0acf646a4bea56ebb9958246b");
    nodeList.add("9845d31972b94a27917d00f244240d99");
    List<Map<String, Object>> auditOpinionList = dao.findAuditOpinionList(dataId);
    for (Map<String, Object> auditOpinion : auditOpinionList) {
      nodeListOpinion.add(String.valueOf(auditOpinion.get("nodeId")));
      if (StringUtils.isBlank(auditOpinion.get("opinionText"))) {
        auditOpinion.put("opinionText", null);
      }
      if (StringUtils.isBlank(auditOpinion.get("status"))) {
        auditOpinion.put("status", "");
      }
      if (StringUtils.isBlank(auditOpinion.get("date"))) {
        auditOpinion.put("date", null);
      }
    }
    nodeList.removeAll(nodeListOpinion);
    for (String nodeId : nodeList) {
      Map opioionMap = new HashMap();
      opioionMap.put("nodeId", nodeId);
      opioionMap.put("opinionText", null);
      opioionMap.put("status", "");
      opioionMap.put("date", null);
      auditOpinionList.add(opioionMap);
    }

    return auditOpinionList;
  }

  //获取归口部门意见
  public List<Map<String, Object>> getDeptLeaderOpinon(String dataId) {
    List<String> nodeIdList=new ArrayList<>();
    nodeIdList.add("AD643EFE68A14E34B74A4E4F43C81B70");
   return  dao.getDeptLeaderOpinon(dataId,nodeIdList);
  }
}
